package service_impl

{{- $Name := .Name  }}
import (
	"context"
    "github.com/liuxd6825/dapr-go-ddd-sdk/errors"
	"{{.Namespace}}/pkg/query-service/domain/{{.aggregate_name}}/view"
	"{{.Namespace}}/pkg/query-service/infrastructure/test"
	"testing"
)

func Test{{.Name}}QueryDomainServiceImpl(t *testing.T)  {
	test.InitQuery()

	id := randomutils.UUID()
	tenantId := test.TenantId
	ctx := context.Background()

	service := new{{.Name}}QueryDomainService()
	new{{.Name}} := view.New{{.Name}}View()
	up{{.Name}} := view.New{{.Name}}View()

    t.Run("Create()", func(t *testing.T) {
    {{- range $name, $property := .AllViewProperties}}
        {{- if not $property.IsArray }}
        new{{$Name}}.{{$name}} = {{$property.GoRepositoryTestValue}}
        {{- end }}
    {{- end}}

		if err := service.Create(ctx, new{{.Name}}); err != nil {
			t.Error(err)
			return
		}

        qry := &query.{{.Name}}FindByIdQuery{
            TenantId: tenantId,
            Id:       id,
        }
		if found{{.Name}}, ok, err := service.FindById(ctx, qry); err != nil {
			t.Error(err)
		} else if !ok {
			t.Error(errors.New("not found"))
		} else if err := new{{.Name}}.Equal(found{{.Name}}); err!=nil {
            t.Error(err)
        }
	})

	t.Run("Update()", func(t *testing.T) {
    {{- range $name, $property := .AllViewProperties}}
        {{- if not $property.NameIsId }}
        {{- if not $property.IsArray }}
        up{{$Name}}.{{$name}} = {{$property.GoRepositoryTestValue}}
        {{- end }}
        {{- end }}
    {{- end }}

		if err := service.Update(ctx, up{{.Name}}); err != nil {
			t.Error(err)
			return
		}

        qry := &query.{{.Name}}FindByIdQuery{
            TenantId: tenantId,
            Id:       id,
        }
		if found{{.Name}}, ok, err := service.FindById(ctx, qry); err != nil {
			t.Error(err)
		} else if !ok {
			t.Error(errors.New("not found"))
		} else if err := up{{.Name}}.Equal(found{{.Name}}); err!=nil {
            t.Error(err)
        }

	})

	t.Run("FindAll()", func(t *testing.T) {
        qry := &query.{{.Name}}FindAllQuery{
            TenantId: tenantId,
        }
		if list, ok, err := service.FindAll(ctx, qry); err != nil {
			t.Error(err)
			return
		} else if ok {
			_ = test.PrintJson(t, "FindAll", list);
		}  else {
            t.Error("not found")
        }
	})

	t.Run("FindByIds()", func(t *testing.T) {
        qry := &query.{{.Name}}FindByIdsQuery{
            TenantId: tenantId,
            Ids:      []string{id, "a832a377-1177-4168-89b9-b8812ea5417b"},
        }
		if list, ok, err := service.FindByIds(ctx, qry); err != nil {
			t.Error(err)
			return
		} else if ok {
            _ = test.PrintJson(t, "FindByIds", list)
		} else {
            t.Error("not found")
        }
	})

    t.Run("FindPagingQuery()", func(t *testing.T) {
		qry := &query.{{.Name}}FindPagingQuery{
			TenantId: tenantId,
			Sort:     "{{.AllViewProperties.Names}}",
			Filter:   fmt.Sprintf("id=='%v'", up{{.Name}}.Id),
		}

        if res, isFound, err := service.FindPaging(ctx, qry); err != nil {
            t.Error(err)
        } else  if isFound {
			_ = test.PrintJson(t, "FindPagingQuery", res)
        } else {
            t.Error("not found")
        }
    })

	t.Run("DeleteById()", func(t *testing.T) {
		if err := service.DeleteById(ctx, up{{.Name}}.TenantId, up{{.Name}}.Id); err != nil {
			t.Error(err)
			return
		}
	})

	t.Run("DeleteAll()", func(t *testing.T) {
		if err := service.DeleteAll(ctx, up{{.Name}}.TenantId); err != nil {
			t.Error(err)
			return
		}
	})
}
